/*
Content: Figures E.4a, E.4b. Tables E.4, E.5, E.6, 4
Paper: "Immigrants Return Intentions and Labor Market Behavior when the Home Country is Unsafe"
Authors: Jacopo Bassetto, Teresa Freitas-Monteiro
*/
		
****************************************************************		
*** ROBUSTNESS CHECKS and PLACEBO OUTCOMES
****************************************************************
use "$data/finaldata_terrorsoep_light_final.dta", clear

********************************************************************************	
**# Table E.4: Vary bandwidth and reference point
********************************************************************************		

global preh "prehead(" \begin{tabular}{l*{7}{c} \hline \hline}")"
global prostf   "postfoot(" \hline \hline} \end{tabular} ")"
global opt "b(3) se(3) star(* 0.10 ** 0.05 *** 0.01) nocons nogap label nonotes booktabs collabels(none) alignment(D{.}{.}{-1}) sfmt(3) noconstant"


global top "prehead(" \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi} \begin{tabular}{l*{7}{c}} \hline\hline") posthead("\hline \\ \multicolumn{6}{l}{\textbf{Panel A: 30-day Bandwidth}} \\\\[-1ex]") fragment replace noobs"

global middle1 "posthead(" \\ \multicolumn{6}{l}{\textbf{Panel B: 60-day Bandwidth}} \\\\[-1ex]") fragment append nomtitles nonumbers noobs"

global mid_head "posthead(" \\\\[-1ex]") fragment append nomtitles nonumbers noobs"

global end "posthead("\\ \multicolumn{6}{l}{\textbf{Panel C: 90-day Bandwidth}} \\\\[-1ex]") fragment append nomtitles nonumbers prefoot("\hline") postfoot("\hline\hline \\ \end{tabular}") "



cap erase "$table/main_results_p30_cluster_final.tex"
foreach band in 30 {
	* No controls		 
eststo clear
local i=1					
foreach var in	m5y m4y m3y  {


global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_`var'_p`band',-3,3) & distanceterror_`var'_p`band'!= 0"
global controls "i.soep_corigin_id#i.syear  i.bula i.month#i.syear i.soep_corigin_id#i.syear" 	

cap drop postterrorbd
clonevar postterrorbd=postterror_`var'_p`band'
qui eststo m`i': reghdfe remain_ger_per postterrorbd  if $select_reg_M & sample_terror_`var'==1 , vce(cluster clusterterror_`var'_p`band') absorb($controls) 
local i=`i'+1
}


* With controls	
local i=1				
foreach var in	m5y m4y m3y  {
global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_`var'_p`band',-3,3) & distanceterror_`var'_p`band'!= 0"
global controls "i.soep_corigin_id#i.syear  i.bula i.month#i.syear i.soep_corigin_id#i.syear" 	

cap drop postterrorbd
clonevar postterrorbd=postterror_`var'_p`band'
qui eststo n`i': reghdfe remain_ger_per postterrorbd  i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child if $select_reg_M & sample_terror_`var'==1 , vce(cluster clusterterror_`var'_p`band') absorb($controls)

local i=`i'+1

}
esttab m* n* using "$table/main_results_p30_cluster_final.tex", $opt $top drop(*sex age *ysm *maritstat *educ_aftm2 *child _cons) varlabel(postterrorbd "Post-Terror" ) mgroups("Higher than average of last" "Higher than average of last", ///
            pattern(1 0 0 1 0 0) ///
            prefix(\multicolumn{@span}{c}{) suffix(}) ///
            span erepeat(\cmidrule(lr){@span})) ///
    mtitles("5 years" "4 years" "3 years" "5 years" "4 years" "3 years")

	}
	
foreach band in 60 {
	* No controls		 
eststo clear
local i=1					
foreach var in	m5y m4y m3y  {


global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_`var'_p`band',-3,3) & distanceterror_`var'_p`band'!= 0"
global controls "i.soep_corigin_id#i.syear  i.bula i.month#i.syear i.soep_corigin_id#i.syear" 	
cap drop postterrorbd
clonevar postterrorbd=postterror_`var'_p`band'
qui eststo m`i': reghdfe remain_ger_per postterrorbd  if $select_reg_M & sample_terror_`var'==1 , vce(cluster clusterterror_`var'_p`band') absorb($controls) 
local i=`i'+1
}


* With controls	
local i=1				
foreach var in	m5y m4y m3y  {
global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_`var'_p`band',-3,3) & distanceterror_`var'_p`band'!= 0"
global controls "i.soep_corigin_id#i.syear  i.bula i.month#i.syear i.soep_corigin_id#i.syear" 	
cap drop postterrorbd
clonevar postterrorbd=postterror_`var'_p`band'
qui eststo n`i': reghdfe remain_ger_per postterrorbd  i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child if $select_reg_M & sample_terror_`var'==1 , vce(cluster clusterterror_`var'_p`band') absorb($controls)

local i=`i'+1

}
esttab m* n* using "$table/main_results_p30_cluster_final.tex", $opt $middle1 drop(*sex age *ysm *maritstat *educ_aftm2 *child _cons) varlabel(postterrorbd "Post-Terror" )
	}
	
	
foreach band in 90 {
	* No controls		 
eststo clear
local i=1					
foreach var in	m5y m4y m3y  {


global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_`var'_p`band',-3,3) & distanceterror_`var'_p`band'!= 0"
global controls "i.soep_corigin_id#i.syear  i.bula i.month#i.syear i.soep_corigin_id#i.syear" 	
cap drop postterrorbd
clonevar postterrorbd=postterror_`var'_p`band'
qui eststo m`i': reghdfe remain_ger_per postterrorbd  if $select_reg_M & sample_terror_`var'==1 , vce(cluster clusterterror_`var'_p`band') absorb($controls) 
local i=`i'+1
qui estadd local orig_y "Yes"
qui estadd local date "Yes"
qui estadd local orig_m "Yes"
qui estadd local state "Yes"
qui estadd local indiv "No"
}


* With controls	
local i=1				
foreach var in	m5y m4y m3y  {
global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_`var'_p`band',-3,3) & distanceterror_`var'_p`band'!= 0"
global controls "i.soep_corigin_id#i.syear  i.bula i.month#i.syear i.soep_corigin_id#i.syear" 	
cap drop postterrorbd
clonevar postterrorbd=postterror_`var'_p`band'
qui eststo n`i': reghdfe remain_ger_per postterrorbd  i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child if $select_reg_M & sample_terror_`var'==1 , vce(cluster clusterterror_`var'_p`band') absorb($controls)
local i=`i'+1
qui estadd local orig_y "Yes"
qui estadd local date "Yes"
qui estadd local orig_m "Yes"
qui estadd local state "Yes"
qui estadd local indiv "Yes"

}
esttab m* n* using "$table/main_results_p30_cluster_final.tex", $opt $end drop(*sex age *ysm *maritstat *educ_aftm2 *child _cons) varlabel(postterrorbd "Post-Terror" )  scalars( "N \hline Observations" "orig_y Origin country x Year FE" "date Month FE x Year FE" "orig_m Origin country x Month FE" "state State of Residency FE" "indiv Indiv. Controls") 
	}
	
	
	
	
	
	
********************************************************************************	
**# Table E.5: Additional controls
********************************************************************************	

			
eststo clear
local i=1				
global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_m3y_p90,-3,3) & distanceterror_m3y_p90!= 0 "
global controls "i.soep_corigin_id#i.syear  i.bula i.syear#i.month "
global controls1 "i.soep_corigin_id#i.syear  i.bula i.syear#i.month i.soep_corigin_id#i.ysm_cat" 	
global controls2 "i.soep_corigin_id#i.syear  i.bula i.syear#i.month i.soep_corigin_id#i.age_g" 	
global controls3 "i.soep_corigin_id#i.syear  i.bula i.syear#i.month i.soep_corigin_id#i.sex" 	

qui: reghdfe remain_ger_per postterror_m3y_p90  i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child  if $select_reg_M & sample_terror_m3y==1 , vce(cluster clusterterror_m3y_p90) absorb($controls)
eststo m0
estadd scalar nobs = e(N)
qui estadd local orig_y "Yes"
qui estadd local date "Yes"
qui estadd local orig_m "Yes"
qui estadd local state "Yes"
qui estadd local orig_ysm "Yes"
qui estadd local orig_age "Yes"
qui estadd local orig_gender "Yes"
qui estadd local indiv "Yes"

qui: reghdfe remain_ger_per postterror_m3y_p90  i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child  if $select_reg_M & sample_terror_m3y==1 , vce(cluster clusterterror_m3y_p90) absorb($controls1)
eststo m1
estadd scalar nobs = e(N)
qui estadd local orig_y "Yes"
qui estadd local date "Yes"
qui estadd local orig_m "Yes"
qui estadd local state "Yes"
qui estadd local orig_ysm "Yes"
qui estadd local orig_age "Yes"
qui estadd local orig_gender "Yes"
qui estadd local indiv "Yes"

qui: reghdfe remain_ger_per postterror_m3y_p90  i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child  if $select_reg_M & sample_terror_m3y==1 , vce(cluster clusterterror_m3y_p90) absorb($controls2)
eststo m2
estadd scalar nobs = e(N)
qui estadd local orig_y "Yes"
qui estadd local date "Yes"
qui estadd local orig_m "Yes"
qui estadd local state "Yes"
qui estadd local orig_ysm "Yes"
qui estadd local orig_age "Yes"
qui estadd local orig_gender "Yes"
qui estadd local indiv "Yes"

qui: reghdfe remain_ger_per postterror_m3y_p90  i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child  if $select_reg_M & sample_terror_m3y==1 , vce(cluster clusterterror_m3y_p90) absorb($controls3)
eststo m3
estadd scalar nobs = e(N)
qui estadd local orig_y "Yes"
qui estadd local date "Yes"
qui estadd local orig_m "Yes"
qui estadd local state "Yes"
qui estadd local orig_ysm "Yes"
qui estadd local orig_age "Yes"
qui estadd local orig_gender "Yes"
qui estadd local indiv "Yes"


esttab m* using "$table/main_results_extraFE_final.tex", star(* 0.10 ** 0.05 *** 0.01) noobs se replace b(3) drop(*sex age *ysm *maritstat *educ_aftm2 *child _cons) nogaps mgroups("Higher than average of last" "Higher than average of last", ///
            pattern(1 0 0 0) ///
            prefix(\multicolumn{@span}{c}{) suffix(}) ///
            span erepeat(\cmidrule(lr){@span})) varlabel(postterror_m3y_p90 "Post-Terror")  scalars( "nobs Observations" "orig_y Origin country x Year FE" "date Month FE x Year FE" "orig_m Origin country x Month FE" "state State of Residency FE" "orig_ysm Origin country x YSM G. FE" "orig_age Origin country x Age G. FE" "orig_gender Origin country x Gender FE""indiv Indiv. Controls") nonotes nomtitle sfmt(%9.0fc  %1s %1s %1s %1s %1s %1s %1s %1s)



********************************************************************************	
**# Table E.6: Different Clusters
********************************************************************************	

cap drop cluster1 cluster2 cluster3
egen cluster1= group(soep_corigin_id)
egen cluster2= group(soep_corigin_id syear)
egen cluster3 = group(soep_corigin_id syear month)

eststo clear
local i=1				
global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_m3y_p90,-3,3) & distanceterror_m3y_p90!= 0 "
global controls "i.soep_corigin_id#i.syear  i.bula i.syear#i.month" 	


 qui eststo m1:  reghdfe remain_ger_per postterror_m3y_p90  i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child  if $select_reg_M & sample_terror_m3y==1 , vce(cluster clusterterror_m3y_p90) absorb($controls)
 estadd scalar nobs = e(N)
 qui estadd local orig_y "Yes"
qui estadd local date "Yes"
qui estadd local orig_m "Yes"
qui estadd local state "Yes"
qui estadd local indiv "Yes"
sum	 remain_ger_per if e(sample)==1 & postterror_m3y_p90==0
estadd scalar ymean = r(mean)

 qui eststo m2: reghdfe remain_ger_per postterror_m3y_p90  i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child  if $select_reg_M & sample_terror_m3y==1 , vce(cluster cluster1) absorb($controls)
 estadd scalar nobs = e(N)
  qui estadd local orig_y "Yes"
qui estadd local date "Yes"
qui estadd local orig_m "Yes"
qui estadd local state "Yes"
qui estadd local indiv "Yes"
sum	 remain_ger_per if e(sample)==1 & postterror_m3y_p90==0
estadd scalar ymean = r(mean)

  qui eststo m3:  reghdfe remain_ger_per postterror_m3y_p90  i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child  if $select_reg_M & sample_terror_m3y==1 , vce(cluster cluster2) absorb($controls)
estadd scalar nobs = e(N)
	 qui estadd local orig_y "Yes"
qui estadd local date "Yes"
qui estadd local orig_m "Yes"
qui estadd local state "Yes"
qui estadd local indiv "Yes"
sum	 remain_ger_per if e(sample)==1 & postterror_m3y_p90==0
estadd scalar ymean = r(mean)

esttab m* using "$table/main_results_vary_SE_cluster_rev1.tex", star(* 0.10 ** 0.05 *** 0.01) se noobs replace b(3) drop(*sex age *ysm *maritstat *educ_aftm2 *child _cons) nogaps nonotes  nogaps mgroups("Standard errors clustered at:", ///
            pattern(1 0 0 ) ///
            prefix(\multicolumn{@span}{c}{) suffix(}) ///
            span erepeat(\cmidrule(lr){@span})) varlabel(postterror_m3y_p90 "Post-Terror")  mtitle("\shortstack{Event\\level}" "\shortstack{Country of\\Origin}" "\shortstack{Country O.\\x Year}") scalars( "nobs Observations"   "ymean Mean intention to stay"   "orig_y Origin country x Year FE" "date Month FE x Year FE" "orig_m Origin country x Month FE" "state State of Residency FE" "indiv Indiv. Controls") sfmt(%9.0fc %9.3f %1s %1s %1s %1s %1s)


********************************************************************************	
**# Figure E.4a: Leave-one-out  with years
********************************************************************************	

foreach var in	 m3y  {

global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_`var'_p90,-3,3) & distanceterror_`var'_p90!= 0"
global controls "i.soep_corigin_id#i.syear  i.bula i.month#i.syear"

cap drop corigin_mainreg
gen corigin_mainreg = soep_corigin_id

forvalues i = 2000/2018 {


qui  eststo exclude`i':  reghdfe remain_ger_per postterror_`var'_p90  i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child if $select_reg_M & sample_terror_`var'==1  & syear != `i', vce(cluster clusterterror_m3y_p90) absorb($controls)

}
eststo baseline:  reghdfe remain_ger_per postterror_`var'_p90  i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child if $select_reg_M & sample_terror_`var'==1  , vce(cluster clusterterror_m3y_p90) absorb($controls)			
}



coefplot exclude2000 exclude2001 exclude2002 exclude2003 exclude2004 exclude2005 exclude2006 exclude2007 exclude2008 exclude2009 exclude2010 exclude2011 exclude2012 exclude2013 exclude2014 exclude2015 exclude2016 exclude2017 exclude2018, ///
keep(postterror_m3y_p90) xscale(r(-0.05(0.05)0.2)) xlabel(-0.05(0.05)0.2, nogrid) graphregion(color(white)) nooffsets aseq swapnames legend(off)  ///
xline(0, lcolor(cranberry)) recast(scatter) msize(small) mcolor(navy) ciopts(recast(rspike) color(navy)) /// 
coeflabels(exclude2000 = "2000" exclude2001 = "2001" exclude2002 = "2002" exclude2003 = "2003" exclude2004 = "2004" exclude2005 = "2005" exclude2006 = "2006" exclude2007 = "2007" exclude2008 = "2008" exclude2009 = "2009" ///
exclude2010 = "2010" exclude2011 = "2011" exclude2012 = "2012" exclude2013 = "2013" exclude2014 = "2014" exclude2015 = "2015" exclude2016 = "2016" exclude2017 = "2017" exclude2018 = "2018", labsize(small))   xsize(5)

graph save "$graph/exclude_year2_rev1.gph", replace
graph export "$graph/exclude_year2_rev1.png", replace


********************************************************************************	
**# Figure E.4b: Leave-one-out  with countries
********************************************************************************	

cap drop corigin_mainreg
gen corigin_mainreg = soep_corigin_id


foreach var in	 m3y  {
global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_`var'_p90,-3,3) & distanceterror_`var'_p90!= 0"
global controls "i.soep_corigin_id#i.syear  i.bula i.month#i.syear"

qui eststo baseline:  reghdfe remain_ger_per postterror_`var'_p90  i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child if $select_reg_M & sample_terror_`var'==1  , vce(cluster clusterterror_m3y_p90) absorb($controls)	

gen sample_soep = e(sample)

}





foreach var in	 m3y  {
global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_`var'_p90,-3,3) & distanceterror_`var'_p90!= 0"
global controls "i.soep_corigin_id#i.syear  i.bula i.month#i.syear"

levelsof corigin_mainreg, 
foreach v in `r(levels)' {
qui eststo exclude`v':  reghdfe remain_ger_per postterror_`var'_p90  i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child if $select_reg_M & sample_terror_`var'==1  & soep_corigin_id != `v', vce(cluster clusterterror_m3y_p90) absorb($controls)

}
}


coefplot baseline exclude2 exclude4 exclude5 exclude6 exclude11 exclude18 exclude21 exclude22 exclude32 exclude60 exclude67 exclude74 exclude120 exclude121 exclude140 exclude165, ///
keep(postterror_m3y_p90) xscale(r(-0.05(0.05)0.2)) xlabel(-0.05(0.05)0.2, nogrid)  graphregion(color(white)) nooffsets aseq swapnames legend(off) ///
xline(0, lcolor(cranberry)) recast(scatter) msize(small) mcolor(navy) ciopts(recast(rspike) color(navy)) /// 
coeflabels(baseline = "Baseline" exclude2 = "Turkey" exclude4 = "Greece" exclude5 = "Italy" exclude6 = "Spain" exclude11 = "France" exclude18 = "USA" exclude21 = "Romania" exclude22 = "Poland" exclude32 = "Russia" ///
exclude60 = "Iraq" exclude67 = "Morocco" exclude74 = "Kazakhstan" exclude120 = "Bosnia-Herzegovina" exclude121 = "Macedonia" exclude140 = "Albania" exclude165 = "Serbia", labsize(small))  xsize(5)

graph save "$graph/robust_excludecorigin_remain_terror_final.gph", replace
graph export "$graph/robust_excludecorigin_remain_terror_final.pdf", as(pdf) replace

				

					
********************************************************************************	
**# Table 4: Placebo outcomes
********************************************************************************	

eststo clear
local i=1	

	foreach y in  worried_mighate worried_crime worried_econdev worried_environ {				
foreach var in m3y  {

	global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_`var'_p90,-3,3) & distanceterror_`var'_p90!= 0"
	global controls "i.soep_corigin_id#i.syear i.bula i.month#i.syear" 	

qui eststo n`i': reghdfe `y' postterror_`var'_p90  i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child if $select_reg_M & sample_terror_`var'==1 , vce(cluster clusterterror_m3y_p90) absorb($controls)
estadd scalar nobs = e(N)
	 qui estadd local orig_y "Yes"
qui estadd local date "Yes"
qui estadd local orig_m "Yes"
qui estadd local state "Yes"
qui estadd local indiv "Yes"
local i=`i'+1

		
}
}

esttab  n* using "$table/placebooutcomes_final.tex", star(* 0.10 ** 0.05 *** 0.01) replace b(3) se(3) drop(*sex age *ysm *maritstat *educ_aftm2 *child _cons) nocons  nogaps nonotes noobs mgroups("Higher than average of last 3 years", ///
            pattern(1 0 0 0) ///
            prefix(\multicolumn{@span}{c}{) suffix(}) ///
            span erepeat(\cmidrule(lr){@span})) varlabel(postterror_m3y_p90 "Post-Terror")  mtitle("Hostility Foreign." "Crime in Ger." "Econ. Develop." "Environment" ) scalars( "nobs Observations"  "orig_y Origin country x Year FE" "date Month FE x Year FE" "orig_m Origin country x Month FE" "state State of Residency FE" "indiv Indiv. Controls") sfmt(%9.0fc %1s %1s %1s %1s %1s)
	 

********************************************************************************	
**# Table E.7: Alternative events - Armed conflict (UPPSALA)
********************************************************************************	 

rename *armedconflict* *armedc*


* CONSTRUCT RELEVANT VARIABLES FOR THE REGRESSION (SAME AS TERROR EVENTS)
foreach  event in armedc {
foreach var in	 m3y {
foreach x in	90  {
egen cluster`event'_m3y_p90=group(relevant`event'_m3y_p90)
}
}
}


foreach  event in armedc {
foreach var in	 m3y {
foreach x in	90  {
* Sum by country of origin, event date and pre-porst, sum number of interviews
cap drop max`event'_`var'_p`x'  
cap drop  min`event'_`var'_p`x'
cap drop  restrict`event'_`var'_p`x'
cap drop countpre`event'_`var'_p`x' countpost`event'_`var'_p`x'
qui bys soep_corigin_id  relevant`event'_`var'_p`x' : egen max`event'_`var'_p`x'  = max(distance`event'_`var'_p`x')
qui bys soep_corigin_id  relevant`event'_`var'_p`x' : egen min`event'_`var'_p`x'  = min(distance`event'_`var'_p`x')

qui bys soep_corigin_id  relevant`event'_`var'_p`x' : egen countpre`event'_`var'_p`x'  = count(post`event'_m3y_p90) if post`event'_m3y_p90==0
qui bys soep_corigin_id  relevant`event'_`var'_p`x' : egen countpost`event'_`var'_p`x'  = count(post`event'_m3y_p90) if post`event'_m3y_p90==1

g restrict`event'_m3y_p90=1 if max`event'_`var'_p`x'<0 & max`event'_`var'_p`x'!=.
replace restrict`event'_m3y_p90=1 if min`event'_`var'_p`x'>0 & min`event'_`var'_p`x'!=.
replace restrict`event'_m3y_p90=1 if countpre`event'_`var'_p`x' <5 & countpre`event'_`var'_p`x' !=. // there are many events with less than 4 obs on each side of pre-post, including 0, which is not used in the estimates. This makes estimation extremely unstable because of the FE
replace restrict`event'_m3y_p90=1 if countpost`event'_`var'_p`x'<5 & countpost`event'_`var'_p`x'!=. // there are many events with less than 4 obs on each side of pre-post, including 0, which is not used in the estimates. This makes estimation extremely unstable because of the FE
cap drop max`event'_`var'_p`x'  
cap drop  min`event'_`var'_p`x'
cap drop countpre`event'_`var'_p`x' countpost`event'_`var'_p`x'
}
}
}

 label var postarmedc_m3y_p90 "Post-Conflict"

 
* RESTRICT TO A CONSTANT SAMPLE

eststo clear
local i=1

foreach  event in armedc {
global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distance`event'_m3y_p90,-3,3) & distance`event'_m3y_p90!= 0 &  restrict`event'_m3y_p90!=1"
global controls "i.soep_corigin_id#i.syear  i.bula i.syear#i.month" 	

qui: reghdfe remain_ger_per post`event'_m3y_p90  i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child if $select_reg_M, vce(cluster cluster`event'_m3y_p90) absorb($controls)
cap drop  sample_`event'_m3y
g sample_`event'_m3y=e(sample)
 eststo m`i'
 local i=1+`i'

}

* MAIN REGRESSION

eststo clear
local i=1

foreach  event in armedc {			
foreach var in  m3y   {

global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distance`event'_`var'_p90,-3,3) & distance`event'_`var'_p90!= 0"
global controls "i.soep_corigin_id#i.syear i.bula i.syear#i.month" 	

qui  eststo m`i': reghdfe remain_ger_per post`event'_`var'_p90  if $select_reg_M & sample_`event'_`var'==1, vce(cluster cluster`event'_`var'_p90) absorb($controls)
 estadd scalar nobs = e(N)
	 qui estadd local orig_y "Yes"
qui estadd local date "Yes"
qui estadd local orig_m "Yes"
qui estadd local state "Yes"
qui estadd local indiv "No"
sum	 remain_ger_per if e(sample)==1 & postterror_m3y_p90==0
estadd scalar ymean = r(mean)
local i=`i'+1

qui  eststo m`i':  reghdfe remain_ger_per post`event'_`var'_p90  i.sex c.age c.ysm##c.ysm  if $select_reg_M & sample_`event'_`var'==1 , vce(cluster cluster`event'_`var'_p90) absorb($controls)
 estadd scalar nobs = e(N)
	 qui estadd local orig_y "Yes"
qui estadd local date "Yes"
qui estadd local orig_m "Yes"
qui estadd local state "Yes"
qui estadd local indiv "Some"
sum	 remain_ger_per if e(sample)==1 & postterror_m3y_p90==0
estadd scalar ymean = r(mean)
local i=`i'+1

qui  eststo m`i':  reghdfe remain_ger_per post`event'_`var'_p90  i.sex c.age c.ysm##c.ysm i.maritstat  i.child  if $select_reg_M & sample_`event'_`var'==1 , vce(cluster cluster`event'_`var'_p90) absorb($controls)
 estadd scalar nobs = e(N)
	 qui estadd local orig_y "Yes"
qui estadd local date "Yes"
qui estadd local orig_m "Yes"
qui estadd local state "Yes"
qui estadd local indiv "Some"
sum	 remain_ger_per if e(sample)==1 & postterror_m3y_p90==0
estadd scalar ymean = r(mean)
local i=`i'+1

qui  eststo m`i':  reghdfe remain_ger_per post`event'_`var'_p90  i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child  if $select_reg_M & sample_`event'_`var'==1 , vce(cluster cluster`event'_`var'_p90) absorb($controls)
 estadd scalar nobs = e(N)
	 qui estadd local orig_y "Yes"
qui estadd local date "Yes"
qui estadd local orig_m "Yes"
qui estadd local state "Yes"
qui estadd local indiv "Yes"
sum	 remain_ger_per if e(sample)==1 & postterror_m3y_p90==0
estadd scalar ymean = r(mean)
local i=`i'+1
}
}

esttab m* using "$table/mainarmedconflict_final.tex", star(* 0.10 ** 0.05 *** 0.01) replace b(3) se(3) drop(*sex age *ysm *maritstat *educ_aftm2 *child _cons) nocons  nogaps nonotes noobs nomtitle mgroups("Higher than average of last 3 years", ///
            pattern(1 0 0 0) ///
            prefix(\multicolumn{@span}{c}{) suffix(}) ///
            span erepeat(\cmidrule(lr){@span})) varlabel(postarmedc_m3y_p90 "Post-Conflict")  scalars( "nobs Observations" "ymean Mean intention to stay"  "orig_y Origin country x Year FE" "date Month FE x Year FE" "orig_m Origin country x Month FE" "state State of Residency FE" "indiv Indiv. Controls") sfmt(%9.0fc %9.3f %1s %1s %1s %1s %1s)



